package com.szholly.pro.watergas_server.ps.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.ITable;
import com.szholly.data.general.ITableFactory;
@Controller
@RequestMapping("/SupplyWaterController/*")
public class SupplyWaterController extends BaseController { 
	protected Logger logger = LoggerFactory.getLogger(getClass());
	/**
	 * 获取供水数据比对数据
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/getComparisonData")
	public void getComparisonData(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String sql = " select t4.C_USEREALNAME AS USER_NAME,t3.name,t1.* from data_comparison t1 "
				+ " left join operation_task t2 on t1.task_code=t2.task_code "
				+ " left join BC_SITE_INFO t3 on t2.station_id=t3.exp_no "
				+ " left join safe_user t4 on t1.user_id=t4.s_userid where 1=1 ";
		String station_id = RequestUnit.getString("station_id");
		String task_code = RequestUnit.getString("task_code");
		if (station_id != null && !station_id.equals("")) {
			sql += " and t2.station_id ='" + station_id + "'";
		}
		if (task_code != null && !task_code.equals("")) {
			sql += " and t2.task_code ='" + task_code + "'";
		}
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		String result = DataUtil.getPageSelectJson(sql, parms,pTable);
		outString(result);
	}
	/**
	 * 获取供水异常处理数据
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/getExceptionHandlingData")
	public void getExceptionHandlingData(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String sql = "select t4.c_userealname as USER_NAME,d2.s_domaincaption as REASON_NAME,t3.name as STATION_NAME,t1.* from OP_ALAR_PROC t1\n" +
						"left join operation_task t2 on t1.task_code=t2.task_code\n" + 
						"left join BC_SITE_INFO t3 on t2.station_id=t3.exp_no\n" + 
						"left join safe_user t4 on t1.solved_by=t4.s_userid\n" + 
						"left join (select do.s_domaincode, do.s_domaincaption from data_defulatdomain do  where do.s_domainname = '供水异常故障项') d2 on t1.EXCEPTION_REASON = d2.s_domaincode\n" + 
						"where 1=1 ";

		String station_id = RequestUnit.getString("station_id");
		String task_code = RequestUnit.getString("task_code");
		if (station_id != null && !station_id.equals("")) {
			sql += " and t2.station_id ='" + station_id + "'";
		}
		if (task_code != null && !task_code.equals("")) {
			sql += " and t1.task_code ='" + task_code + "'";
		}
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		String result = DataUtil.getPageSelectJson(sql, parms,pTable);
		outString(result);
	}
	/**
	 * 获取供水耗材/备件更换
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/getMaterialReplaceData")
	public void getMaterialReplaceData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		List<GlobalParameter> listParam = new ArrayList<GlobalParameter>();
		String sql = " select f.*, t1.NAME as WWTP_NAME,t1.OUNAME, t2.NAME as STATION_NAME, t2.FLOW_NAME, t3.NAME as EQUIPMENT_NAME, t3.DEVICE_TYPENUM, t4.c_userealname as USER_NAME, "
				+ " d1.S_DOMAINCAPTION as MATERIAL_NAME_NAME, d2.S_DOMAINCAPTION as MATERIAL_UNIT_NAME, d3.S_DOMAINCAPTION as REPLACE_RESULT_NAME  "
				+ " from OP_MATE_REPLACE f " + " left join OPERATION_TASK t on ( t.TASK_CODE = f.TASK_ID ) "
				+ " left join (select pwa.name,pwa.exp_no,ou.name as OUNAME from BC_ENTERPRISE_ATTR pwa left join operations_unit ou on pwa.operator=ou.guid) t1 on ( t1.EXP_NO = t.WWTP_ID ) "
				+ " left join BC_SITE_INFO t2 on ( t2.EXP_NO = t.STATION_ID ) "
				+ " left join BC_SITE_DEVICE_RPT  t3 on ( t3.DEVICE_NUM = f.DEVICE_NUMBER ) "
				+ " left join safe_user t4 on ( t4.s_userid = f.REPLACE_PERSON ) "
				+ " left join DATA_DEFULATDOMAIN d1 on ( d1.S_DOMAINNAME = '供水耗材名称' AND d1.S_DOMAINCODE = f.MATERIAL_TYPE) "
				+ " left join DATA_DEFULATDOMAIN d2 on ( d2.S_DOMAINNAME = 'OPERATION_MATERIAL_UNIT' AND d2.S_DOMAINCODE = f.MATERIAL_UNIT) "
				+ " left join DATA_DEFULATDOMAIN d3 on ( d3.S_DOMAINNAME = 'OPERATION_REPLACE_RESULT' AND d3.S_DOMAINCODE = f.REPLACE_RESULT) "
				+ " where 1=1 ";
		String station_id = RequestUnit.getString("station_id");
		String task_code = RequestUnit.getString("task_code");
		if (task_code != null && !task_code.equals("")) {
			sql += " and f.TASK_ID = @task_code";
			listParam.add(new GlobalParameter("task_code", task_code));
		} 
			if (station_id != null && !station_id.equals("")) {
				sql += " and t2.exp_no = @station_id";
				listParam.add(new GlobalParameter("station_id", station_id));
			}
			
			
		
		sql += " order by f.REPLACE_DATE desc";
		String result = DataUtil.getPageSelectJson(sql, listParam);
		outString(result);
	}
}
